﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.DemandPlan
{
    public partial class BidPlanSubmit : System.Web.UI.Page
    {
        Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }

        }

        //招标计划提报
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            string batchnum = this.txt_batchnum.Text;
            string bagnum = this.txt_bagnum.Text;
            string npmethod = this.ddl_npmethod.SelectedItem.Text;
            string bagname = this.txt_bagname.Text;

            if (string.IsNullOrEmpty(batchnum))
            {
                Maticsoft.Common.MessageBox.Show(this.Page,"请填写招标批次");
                return;
            }
            if (string.IsNullOrEmpty(bagnum))
            {
                Maticsoft.Common.MessageBox.Show(this.Page, "请填写包号");
                return;
            }
            if (string.IsNullOrEmpty(bagname))
            {
                Maticsoft.Common.MessageBox.Show(this.Page, "请填写包名");
                return;
            }
           


            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int dpid = (Convert.ToInt32(Request.Params["id"]));

                lcs.BLL.demandplaninfo dm = new BLL.demandplaninfo();
                lcs.Model.demandplaninfo dp = dm.GetModel(dpid);

                dp.bagnum = bagnum;
                dp.batchnum = batchnum;
                dp.npmethod = npmethod;
                dp.bagname = bagname;

                if (dm.UpdateModelBidSubmit(dp))
                {
                    List<Maticsoft.Model.documentFlowInfo> li = new Maticsoft.BLL.documentFlowInfo().GetModelList(" document_id = " + dpid);

                    if (li.Count == 1)
                    {
                        Maticsoft.Model.documentFlowInfo d = li[0];

                        string reason = "同意";

                        int person_id = 0;
                        if (Session["login"] != null)
                        {
                            DataSet ds = Session["login"] as DataSet;

                            person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);
                        }

                        //获取当前审批人审批节点号，transfer的undefine4记录的是该由哪个审批人审批的节点号，如果要修改自身的workFlow，则节点号就是undefine4
                        int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                        int flow_node_id_now = 0;

                        flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id.Value, flow_node_num_now);

                        if (wm.UpdateW(person_id, d.document_id.Value, 1, reason, DateTime.Now, flow_node_id_now, flow_node_num_now))//修改本身意见为同意,添加处理意见、处理时间
                        {
                            int flow_node_id = wm.GetFlow_node_id(person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now),d.flow_id.Value);

                            string str = wm.Submit_judge(flow_node_id, person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now));//得到审核状态

                            if (str == "同意")//1、如果是会签非尾节点同意，插入下一节点
                            {
                                lcs.BLL.Tools.DealWorkFlow(d, flow_node_id, person_id);

                            }

                            if (str == "尾节点同意")//2、如果是会签尾节点同意
                            {
                                lcs.BLL.Tools.DealLast(d, person_id);

                            }

                            if (str == "非会签")//3、如果是非会签非尾节点，插入下一结点
                            {
                                lcs.BLL.Tools.DealWorkFlow(d, flow_node_id, person_id);
                            }

                            if (str == "尾节点非会签")
                            {
                                lcs.BLL.Tools.DealLast(d, person_id);
                            }
                        }

                        Maticsoft.Common.MessageBox.ShowAndRedirect(this.Page, "提交成功！", "ListBidPlanSubmit.aspx");
                    }

                    
                }

            }
        }
    }
}